﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.IO;
using System.Globalization;

namespace Pixysoft.Framework.Logs.Core
{
    class RecordCommand : IRecordCommand
    {
        string path = null;

        string name = null;

        string postfix = null;

        string elementName = null;

        bool crossFiles = false;

        IXmlCommand command = null;


        string id;

        DateTime rangeFrom = DateTime.MinValue;

        DateTime rangeTo = DateTime.MaxValue;//bug


        public RecordCommand(string path, string name, string postfix, string elementName, bool crossFiles)
        {
            this.path = path;
            this.name = name;
            this.postfix = postfix;
            this.elementName = elementName;
            this.crossFiles = crossFiles;

            this.command = XmlManager.CreateCommand(path + name + postfix);

            this.command.Element = elementName;
        }


        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public DateTime RangeFrom
        {
            get
            {
                return rangeFrom;
            }
            set
            {
                rangeFrom = value;
            }
        }

        public DateTime RangeTo
        {
            get
            {
                return rangeTo;
            }
            set
            {
                rangeTo = value;
            }
        }

        public Pixysoft.Framework.Configurations.IXmlConstraintCollection Constraints
        {
            get
            {
                return command.Constraints;
            }
        }

        public IDocument ExecuteReader()
        {
            if (!string.IsNullOrEmpty(id))
                command.Constraints.And(StringHelper.default_attribute_id).Equal(id);

            if (rangeFrom != DateTime.MinValue)
                command.Constraints.And<DateTime>(StringHelper.default_attribute_time).GreatEqual(rangeFrom, StringHelper.default_log_dateformat);

            if (rangeTo != DateTime.MaxValue)
                command.Constraints.And<DateTime>(StringHelper.default_attribute_time).LessEqual(rangeTo, StringHelper.default_log_dateformat);

            IDocument doc = new Document(path, name, postfix);

            foreach (string file in GetSearchFiles())
            {
                IXmlTree tree = GetXmlTree(file);

                foreach (IXmlNode node in tree.Node)
                {
                    doc.Records.Add(node);
                }
            }

            doc.Records.Sort();

            return doc;
        }

        private List<string> GetSearchFiles()
        {
            List<string> list = new List<string>();

            if (File.Exists(path + name + postfix))
                list.Add(path + name + postfix);

            if (!crossFiles)
            {
                return list;
            }

            foreach (string file in GetCrossFiles())
            {
                list.Add(file);
            }

            return list;
        }

        /// <summary>
        /// 取得区间内的搜索文本
        /// </summary>
        /// <returns></returns>
        private List<string> GetCrossFiles()
        {
            if (!Directory.Exists(path))
                throw new Exception("file path do not exist." + path);

            List<string> list = new List<string>();

            bool outbounded = false;

            foreach (string desFileWithPathName in Directory.GetFiles(path))
            {
                string desFileFullName = StringFilter.GetFileName(desFileWithPathName);

                if (string.IsNullOrEmpty(desFileFullName))
                    continue;

                if (!desFileFullName.EndsWith(postfix, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!desFileFullName.StartsWith(name, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (desFileFullName.Equals(name + postfix, StringComparison.OrdinalIgnoreCase))
                    continue;


                DateTime lasttime = DateTime.MinValue;

                if (!StringHelper.TryParseFileSequence(desFileFullName, out lasttime))
                    continue;

                if (lasttime < rangeFrom)
                    continue;

                if (lasttime < rangeTo)
                {
                    list.Add(desFileWithPathName);

                    continue;
                }

                if (lasttime > rangeTo && !outbounded)
                {
                    outbounded = true;

                    list.Add(desFileWithPathName);

                    continue;
                }

                if (lasttime > rangeTo && outbounded)
                {
                    break;
                }
            }

            list.Sort();

            return list;
        }

        /// <summary>
        /// 取得搜索结果
        /// </summary>
        /// <returns></returns>
        private IXmlTree GetXmlTree(string file)
        {
            command.Filepath = file;

            IXmlTree tree = command.ExecuteReader();

            return tree;
        }
    }
}
